package edu.taru.controller;

import edu.taru.pojo.Header;
import edu.taru.pojo.Menu;
import edu.taru.pojo.User;
import edu.taru.service.HeaderService;
import edu.taru.service.UserService;
import edu.taru.util.CheckedTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {

    private UserService service;
    private HeaderService head;
    @Autowired
    public UserController(UserService service,HeaderService head){
        this.service = service;
        this.head = head;
    }

    /**
     * 登录
     * @param userName
     * @param password
     * @param attributes
     * @param request
     * @return
     */
    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public String  login (@RequestParam(value = "userName",defaultValue = "") String userName,
                          @RequestParam(value = "password",defaultValue = "") String password ,
                          RedirectAttributes attributes, HttpServletRequest request){
        User user = service.getUserByUserNamePassword(userName,password);
        Header photo=head.selectPhoto(user.getUserId());
        HttpSession session = request.getSession();
        if(user != null){
            session.setAttribute("userName",userName);
            session.setAttribute("userId",user.getUserId());
            attributes.addAttribute("userId",user.getUserId());
            session.setAttribute("picture",photo);
            return "redirect:/user/index";
        }else{
            attributes.addAttribute("loginerror","用户名或密码错误");
            return "login";
        }
    }

    /**
     * 初始化请求，登录之后，用于查询角色和权限
     * @param session
     * @return
     */
    @RequestMapping(value = "/user/index",method = RequestMethod.GET)

    public String  indexPage(HttpSession session){
        Header photo=head.selectPhoto((String) session.getAttribute("userId"));
        session.setAttribute("picture",photo);

        User user = service.getUserByUserId((String) session.getAttribute("userId"));
        session.setAttribute("user",user);
        //登录完成之后，把权限codes存入list中
        List<String> codes = new ArrayList<>();
        for(int i = 0; i < user.getRoles().size(); i++){
            List<Menu> menus = user.getRoles().get(i).getMenus();
            for(int j = 0; j < menus.size(); j++){
                codes.add(menus.get(j).getMenuCode());
            }
        }
        session.setAttribute("codes",codes);
        CheckedTag.session = session;
        return "main";
    }

    /**
     * 退出,清除session
     * @return
     */
    @RequestMapping(value = "/user/logout",method = RequestMethod.GET)
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }

    /**
     * 遍历用户
     */
    @RequestMapping(value = "/select/user")
    public String selectUser(HttpSession session){
        List<User> list= service.seletUser();
        session.setAttribute("Use",list);
        return "redirect:/select/Role";
    }
    /**
     * 查询用户信息
     */
    @RequestMapping(value = "/selectUserInfor")
    public String  selectInfor(Integer uid,HttpSession session){
        User user=service.selectUseInfor(uid);
        session.setAttribute("uInfor",user);
        return "alterInfor";
    }
    /**
     * 修改用户信息
     */
    @RequestMapping(value = "/alter/user")
    public String alterUser(User user){
        service.updateUser(user);
        return "main";
    }

}
